package com.example.ticketorder.controller;

import com.example.ticketorder.entity.*;
import com.example.ticketorder.service.SalesService;
import com.example.ticketorder.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TicketController {
    Ticket ticket=new Ticket();
    Sales sales=new Sales();
    Sales sales2=new Sales();
    Date da;
    SaleForm saleForm = new SaleForm();
    @Autowired
    @Qualifier("ticketService")
    TicketService ticketService;

    @RequestMapping("/orders")
    public String loginForm(Model model) {
        System.out.println(ticketService.findAll());
        Iterable<String> ticket1 = ticketService.findDestination();
        for (String s : ticket1) {
            System.out.println(s);
        }
        System.out.println(model);
        model.addAttribute("tickets1",ticketService.findDestination());
        model.addAttribute("tic",ticketService.findAll());
        System.out.println(model);
        return "order";
    }
    @RequestMapping("/see")
    @ResponseBody
    public List<TicketForm> seeSearch(@RequestParam("destination")String destination,
                            @RequestParam("date")Date date) {
        List<TicketForm> list = new ArrayList<TicketForm>();
        System.out.println("进来了3");
        System.out.println(destination);
        System.out.println(date);
        da = date;
        Iterable<String> ticketDirect = ticketService.findScar(destination,date);
        String departure = "厦门";
        String depar = departure;
        String des = "";
        double money =0;
        int i=0;
        for (String s : ticketDirect) {
            while(!des.equals(destination)){
                System.out.println(s);
                des = ticketService.findDest(s,depar);
                System.out.println("这次的目的地是:"+des);
                money+=ticketService.findMoney(s,des);
                System.out.println(depar);
                System.out.println(des);
                depar=des;
            }
            if(des.equals(destination)){
                System.out.println("到达");
                System.out.println("这次的金额为"+money);
                TicketForm ticketForm = new TicketForm();
                ticketForm.setCar(s);
                ticketForm.setDeparture("厦门");
                ticketForm.setDestination(destination);
                ticketForm.setHtimea(ticketService.findHtimea(s,"厦门"));
                ticketForm.setHtimeb(ticketService.findHtimeb(s,destination));
                ticketForm.setPoll(ticketService.findPoll(s));
                ticketForm.setMoney(money);
                ticketForm.setClassify(0);
                System.out.println(ticketForm);
                list.add(ticketForm);
                System.out.println(list);

            }
            money=0;
            des="";
            depar=departure;
        }
        money=0;
        des="";
        depar=departure;
        for (String s : ticketDirect) {
            Iterable<String> Site = ticketService.findSite(s,destination);
            System.out.println(Site);
            //用可以直达目的地的车号所查的站点去查
            for(String site : Site){
                Time timea = ticketService.findHtimea(s,site);
                System.out.println(timea);
                System.out.println(s);
                Iterable<String> zcar = ticketService.findZcar(site,timea,s);
                Iterator it = zcar.iterator();
                System.out.println(zcar);
                System.out.println("正在循环的site:"+site);
                if(it.hasNext()){
                    System.out.println("查出的车号不为空");
                    for(String car:zcar){
                        System.out.println("正在循环的car:"+car);
                        while(!des.equals(site)){

                            des = ticketService.findDest(car,depar);
                            System.out.println("这次的目的地是:"+des);
                            money+=ticketService.findMoney(car,des);
                            System.out.println(depar);
                            System.out.println(des);
                            depar=des;
                        }
                        if(des.equals(site)){
                            TicketForm ticketForm = new TicketForm();
                            ticketForm.setCar(car);
                            ticketForm.setDeparture("厦门");
                            ticketForm.setDestination(site);
                            ticketForm.setHtimea(ticketService.findHtimea(car,"厦门"));
                            ticketForm.setHtimeb(ticketService.findHtimeb(car,site));

                            ticketForm.setPoll(ticketService.findPoll(car));
                            ticketForm.setMoney(money);
                            ticketForm.setClassify(1);
                            System.out.println(ticketForm);
                            list.add(ticketForm);
                            System.out.println(list);
                        }
                        money=0;
                        des="";
                        depar=site;
                        while(!des.equals(destination)){

                            des = ticketService.findDest(s,depar);
                            System.out.println("这次的目的地是:"+des);
                            money+=ticketService.findMoney(s,des);
                            System.out.println(depar);
                            System.out.println(des);
                            depar=des;
                        }
                        if(des.equals(destination)){
                            TicketForm ticketForm = new TicketForm();
                            ticketForm.setCar(s);
                            ticketForm.setDeparture(site);
                            ticketForm.setDestination(destination);
                            ticketForm.setHtimea(ticketService.findHtimea(s,site));
                            ticketForm.setHtimeb(ticketService.findHtimeb(s,destination));
                            ticketForm.setPoll(ticketService.findPoll(s));
                            ticketForm.setMoney(money);
                            ticketForm.setClassify(2);
                            System.out.println(ticketForm);
                            list.add(ticketForm);
                            System.out.println(list);
                        }
                        money=0;
                        des="";
                        depar=departure;
                    }
                }
            }
        }
        System.out.println(list);
        return list;
    }
    @Autowired
    SalesService salesService;
    @RequestMapping("/bars")
    public String seebar(@RequestParam("classify")int classify,
                            @RequestParam("car1")String car1,
                            @RequestParam("car2")String car2,
                            @RequestParam("departure1")String departure1,
                            @RequestParam("departure2")String departure2,
                            @RequestParam("destination1")String destination1,
                            @RequestParam("destination2")String destination2,
                            @RequestParam("Date")Date Dtime,
                            @RequestParam("Htimea1")Time Htimea1,
                            @RequestParam("Htimea2")Time Htimea2,
                            @RequestParam("Htimeb1")Time Htimeb1,
                            @RequestParam("Htimeb2")Time Htimeb2,
                            @RequestParam("polla")int polla,
                            @RequestParam("pollb")int pollb,
                            @RequestParam("mon")double money,
                            @RequestParam("phone")String phone,
                            @RequestParam("Money")double Money,
                            Model model){
        if(classify==0){
            TicketForms ticketForms = new TicketForms();
            List<Ticket> tickets = new ArrayList<Ticket>();
            Ticket ticket1 = new Ticket();
            ticket1.setCar(car1);
            ticket1.setDeparture(departure1);
            ticket1.setDestination(destination1);
            ticket1.setDtime(Dtime);
            ticket1.setHtimea(Htimea1);
            ticket1.setHtimeb(Htimeb1);
            ticket1.setPoll(polla);
            ticket1.setMoney(money);
            tickets.add(ticket1);
            Ticket ticket2 = new Ticket();
            ticket2.setCar(car1);
            ticket2.setDeparture(departure1);
            ticket2.setDestination(destination1);
            ticket2.setDtime(Dtime);
            ticket2.setHtimea(Htimea1);
            ticket2.setHtimeb(Htimeb1);
            ticket2.setPoll(polla);
            ticket2.setMoney(money);
            tickets.add(ticket2);
            ticketForms.setTickets(tickets);
            ticketForms.setClassify(classify);
            ticketForms.setMon(Money);
            ticketForms.setPhone(phone);
            System.out.println("进度条的"+ticketForms);
            model.addAttribute("Forms",ticketForms);
        }else{
            TicketForms ticketForms = new TicketForms();
            List<Ticket> tickets = new ArrayList<Ticket>();
            Ticket ticket1 = new Ticket();
            ticket1.setCar(car1);
            ticket1.setDeparture(departure1);
            ticket1.setDestination(destination1);
            ticket1.setDtime(Dtime);
            ticket1.setHtimea(Htimea1);
            ticket1.setHtimeb(Htimeb1);
            ticket1.setPoll(polla);
            ticket1.setMoney(money);
            tickets.add(ticket1);
            Ticket ticket2 = new Ticket();
            ticket2.setCar(car2);
            ticket2.setDeparture(departure2);
            ticket2.setDestination(destination2);
            ticket2.setDtime(Dtime);
            ticket2.setHtimea(Htimea2);
            ticket2.setHtimeb(Htimeb2);
            ticket2.setPoll(pollb);
            ticket2.setMoney(money);
            tickets.add(ticket2);
            ticketForms.setTickets(tickets);
            ticketForms.setClassify(classify);
            ticketForms.setMon(Money);
            ticketForms.setPhone(phone);
            System.out.println("进度条的"+ticketForms);
            model.addAttribute("Forms",ticketForms);
        }
        return "progressbar";

    }
    @RequestMapping("/tickets")
    public String seeTicket(@RequestParam("classify")int classify,
                          @RequestParam("car1")String car1,
                          @RequestParam("car2")String car2,
                          @RequestParam("departure1")String departure1,
                          @RequestParam("departure2")String departure2,
                          @RequestParam("destination1")String destination1,
                          @RequestParam("destination2")String destination2,
                          @RequestParam("Date")Date Dtime,
                          @RequestParam("Htimea1")Time Htimea1,
                          @RequestParam("Htimea2")Time Htimea2,
                          @RequestParam("Htimeb1")Time Htimeb1,
                          @RequestParam("Htimeb2")Time Htimeb2,
                          @RequestParam("polla")int polla,
                          @RequestParam("pollb")int pollb,
                          @RequestParam("mon")double money,
                          @RequestParam("phone")String phone,
                          @RequestParam("Money")double Money,
                            Model model,
                            HttpServletResponse response)throws IOException {
        System.out.println("看选的票");
        System.out.println(classify);
        System.out.println(car1);
        System.out.println(car2);
        System.out.println(destination1);
        System.out.println(destination2);
        System.out.println(money);
        System.out.println(Money);
        System.out.println(phone);
        List<Sales> sal = new ArrayList<Sales>();
        if(Money>=money){
            System.out.println("钱交够了");
            if(classify==0){
                System.out.println("直达");
                Iterable<String> deps = ticketService.findDeparture(car1,Htimeb1);
                int q=0;
                for(String dep:deps){
                    int z = ticketService.findZpoll(car1,dep);
                    if(z==0){
                        System.out.println("没票了");
                        q=1;
                    }
                }
                if(q==0){
                    System.out.println("票数足够购买");
                    for(String dep:deps){
                        System.out.println("出发地"+dep);
                        ticketService.updatePoll(car1,dep);
                    }
                    //这里是生成订单编号
                    StringBuilder order = new StringBuilder();
                    Random random = new Random();
                    order.append("B");
                    order.append(Dtime.toString());
                    for(int i = 0; i < 3; ++i) {
                        order.append(random.nextInt(10));
                    }
                    System.out.println(order);
                    sales.setOrder_Number(order.toString());
                    sales.setCar(car1);
                    sales.setPhone(phone);
                    System.out.println(Dtime);
                    sales.setDtime(Dtime);
                    sales.setDestination(destination1);
                    sales.setHtimea(Htimea1);
                    sales.setHtimeb(Htimeb1);
                    sales.setMoney(money);
                    salesService.add(sales);
                    sal.add(sales);
                    saleForm.setSales(sal);
                    saleForm.setBalance((Money-money));
                    saleForm.setClassify(classify);
                    System.out.println(saleForm);
                    model.addAttribute("saleForm",saleForm);
                    System.out.println(model);
                }else{
                    response.setContentType("text/html;charset=utf-8");
                    PrintWriter writer = response.getWriter();
                    String ms = "alert('票数不够');history.go(-1)";    //回到上一个页面
                    writer.print("<script type='text/javascript'>" + ms + "</script>");
                    writer.flush();
                    writer.close();
                }
            }else if(classify==2){
                System.out.println("转乘");
                Iterable<String> deps = ticketService.findDeparture(car1,Htimeb1);
                Iterable<String> deps2 = ticketService.findDeparture(car2,Htimeb2);
                int q=0;
                for(String dep:deps){
                    int z = ticketService.findZpoll(car1,dep);
                    if(z==0){
                        System.out.println("没票了");
                        q=1;
                    }
                }
                for(String dep:deps2){
                    int z = ticketService.findZpoll(car2,dep);
                    if(z==0){
                        System.out.println("没票了");
                        q=1;
                    }
                }
                if(q==0){
                    System.out.println("转乘的票数足够购买");
                    for(String dep:deps){
                        System.out.println("出发地"+dep);
                        ticketService.updatePoll(car1,dep);
                    }
                    for(String dep:deps2){
                        System.out.println("出发地2"+dep);
                        ticketService.updatePoll(car2,dep);
                    }
                    //这里是生成订单编号
                    StringBuilder order = new StringBuilder();
                    Random random = new Random();
                    order.append("B");
                    order.append(Dtime.toString());
                    for(int i = 0; i < 3; ++i) {
                        order.append(random.nextInt(10));
                    }
                    System.out.println(order);
                    sales.setOrder_Number(order.toString());
                    sales.setCar(car1);
                    sales.setPhone(phone);
                    System.out.println(Dtime);
                    sales.setDtime(Dtime);
                    sales.setDestination(destination1);
                    sales.setHtimea(Htimea1);
                    sales.setHtimeb(Htimeb1);
                    sales.setMoney(money);
                    //salesService.add(sales);
                    salesService.insertSale(order.toString(),phone,car1,destination1,Dtime,Htimea1,Htimeb1,money);
                    System.out.println("第一趟:"+sales);
                    sal.add(sales);
                    System.out.println(sal);
                    //第二趟
                    sales2.setOrder_Number(order.toString());
                    sales2.setCar(car2);
                    sales2.setPhone(phone);
                    sales2.setDtime(Dtime);
                    sales2.setDestination(destination2);
                    sales2.setHtimea(Htimea2);
                    sales2.setHtimeb(Htimeb2);
                    sales2.setMoney(money);
                    //salesService.add(sales2);
                    salesService.insertSale(order.toString(),phone,car2,destination2,Dtime,Htimea2,Htimeb2,money);
                    System.out.println("第二趟:"+sales2);
                    sal.add(sales2);
                    System.out.println(sal);
                    //salesService.add(sales);
                    saleForm.setSales(sal);
                    saleForm.setBalance((Money-money));
                    saleForm.setClassify(classify);
                    System.out.println(saleForm);
                    model.addAttribute("saleForm",saleForm);
                    System.out.println(model);
                }
            }
        }else{
            System.out.println("钱不够");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            String msg = "alert('所交金额不足');history.go(-1)";    //回到上一个页面
            writer.print("<script type='text/javascript'>" + msg + "</script>");
            writer.flush();
            writer.close();

        }
        return "sales";
    }
    @RequestMapping("/refunds")
    public String refundTicket(Model model) {
        return "refund";
    }
    @RequestMapping("/ref")
    public String refund(@RequestParam("ord")String order,
                         @RequestParam("pho")String phone,
                         Model model) {
        System.out.println("退票中");
        System.out.println(order);
        System.out.println(phone);
        Iterable<Sales> lsal = salesService.findSales(order,phone);
        for(Sales sal:lsal){
            System.out.println(sal);
            Iterable<String> deps = ticketService.findDeparture(sal.getCar(),sal.getHtimeb());
            for(String dep:deps){
                System.out.println("出发地"+dep);
                ticketService.updatePollq(sal.getCar(),dep);
            }
        }
        salesService.deleteSale(order,phone);
        return "refund";
    }
    @RequestMapping("/seerefund")
    @ResponseBody
    public Iterable<Sales> seeRefund(@RequestParam("order")String order,
                            @RequestParam("phone")String phone,
                            Model model) {
        System.out.println(order);
        System.out.println(phone);
        Iterable<Sales> lsal = salesService.findSales(order,phone);
        System.out.println(lsal);
        System.out.println("查询订单信息");
        return lsal;
    }
    @RequestMapping({"/wel"})
    public String success(){
        return "welcome";
    }
    @RequestMapping("/ceshiw")
    public String ceshi(Model model) {

        return "ceshi";
    }
}
